<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      check-strictly
      default-expand-all
      :props="{label:'name'}"
      node-key="id"
    />
    <el-row type=" flex" justify="end">
      <el-button type="primary" size="mini">取消</el-button>
      <el-button type="primary" size="mini" @click="hSubmit">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermission } from '@/api/permission'
import { pinbu } from '@/utils/pinpu'
import { getRoleId, assignPerm } from '@/api/setting'
export default {
  name: 'VueWhRzAssignPermission',
  props: {
    curId: {
      type: String,
      required: true
    }
  },

  data() {
    return {
      list: [],
      permIds: []
    }
  },
  created() {
    this.lodePermission()
  },

  methods: {
    async hSubmit() {
      console.log(0)
      try {
        this.permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.curId, permIds: this.permIds })
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        // 通知父组件关闭弹层
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所有权限点
    async lodePermission() {
      try {
        const res = await getPermission()
        // 获取角色详情
        const defId = await getRoleId(this.curId)
        this.$refs.tree.setCheckedKeys(defId.data.permIds)

        if (res.code !== 10000) return this.$message.error(res.message)
        this.list = pinbu(res.data)
      // 保存数据
      } catch (err) {
        console.log('getPermission', err)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
